﻿namespace SERFLITEMAX20.DataObjects
{
    using System;
    using System.Collections;
    using System.Diagnostics;
    using System.IO;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;

    [XmlRoot("maps", Namespace="", IsNullable=false)]
    public class Maps : IDisposable
    {
        [XmlElement("map", typeof(Map))]
        public ArrayList MapItemsArray = new ArrayList();

        public void Add(int pid, string pname)
        {
            Map map = new Map();
            map.id = pid;
            map.name = pname;
            this.MapItemsArray.Add(map);
        }

        public static Maps Load()
        {
            Maps maps = new Maps();
            if (File.Exists("maps.xml"))
            {
                try
                {
                    XmlTextReader xmlReader = new XmlTextReader("maps.xml");
                    XmlSerializer serializer = new XmlSerializer(maps.GetType());
                    maps = (Maps) serializer.Deserialize(xmlReader);
                    xmlReader.Close();
                }
                catch (Exception exception)
                {
                    Debug.WriteLine(exception.Message);
                    maps = new Maps();
                }
            }
            return maps;
        }

        public void Save()
        {
            XmlTextWriter writer = new XmlTextWriter("maps.xml", Encoding.ASCII);
            new XmlSerializer(base.GetType()).Serialize((XmlWriter) writer, this);
            writer.Close();
        }

        void IDisposable.Dispose()
        {
            this.MapItemsArray.Clear();
            this.MapItemsArray = null;
        }
    }
}

